package com.cndw.kungfu.model.goods.domain;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.vo.goodsvo.GoodsVO;

/**
 * 
 * 合成
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2011-5-30
 */
public class ModeGoodsCompound {

	/**
	 * key(合成宝石时为合成的的等级，合成强化石时为GoodsId)
	 */
	private int key;

	private int level;
	/**
	 * 需要数量
	 */
	private int counts;
	/**
	 * 合成几率
	 */
	private int ratio;
	/**
	 * 所需银两
	 */
	private int asset;

	// 类型3碎片,类型2宝石，类型1强化石
	private int type;

	/**
	 * 合成物品和产生宝石的映射
	 */
	private Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();

	private static Map<Integer, ModeGoodsCompound> gemMap = new HashMap<Integer, ModeGoodsCompound>();

	private static Map<Integer, ModeGoodsCompound> boxMap = new HashMap<Integer, ModeGoodsCompound>();

	public static final int[] goods = new int[] { 103042, 103031, 103032, 103033, 103034, 103035, 103036, 103037, 103038, 103039, 103040, 103041 };

	public static final int[] boxs = new int[] { 103101, 103102, 103103, 103104, 103105, 103106 };

	static {

		// 宝石合成配置
		gemMap.put(0, new ModeGoodsCompound(0, 1, 3, 5, 100, 1000, new int[] { 103046, 103047, 103048, 103049, 103050 }, new int[] { 103051, 103052, 103053, 103054, 103055 }));
		gemMap.put(1, new ModeGoodsCompound(1, 2, 2, 5, 100, 2000, new int[] { 103051, 103052, 103053, 103054, 103055 }, new int[] { 103056, 103057, 103058, 103059, 103060 }));
		gemMap.put(2, new ModeGoodsCompound(2, 3, 2, 5, 100, 3000, new int[] { 103056, 103057, 103058, 103059, 103060 }, new int[] { 103061, 103062, 103063, 103064, 103065 }));
		gemMap.put(3, new ModeGoodsCompound(3, 4, 2, 5, 100, 4000, new int[] { 103061, 103062, 103063, 103064, 103065 }, new int[] { 103066, 103067, 103068, 103069, 103070 }));
		gemMap.put(4, new ModeGoodsCompound(4, 5, 2, 5, 100, 5000, new int[] { 103066, 103067, 103068, 103069, 103070 }, new int[] { 103071, 103072, 103073, 103074, 103075 }));
		gemMap.put(5, new ModeGoodsCompound(5, 6, 2, 5, 100, 6000, new int[] { 103071, 103072, 103073, 103074, 103075 }, new int[] { 103076, 103077, 103078, 103079, 103080 }));
		gemMap.put(6, new ModeGoodsCompound(6, 7, 2, 5, 100, 7000, new int[] { 103076, 103077, 103078, 103079, 103080 }, new int[] { 103081, 103082, 103083, 103084, 103085 }));
		gemMap.put(7, new ModeGoodsCompound(7, 8, 2, 5, 100, 8000, new int[] { 103081, 103082, 103083, 103084, 103085 }, new int[] { 103086, 103087, 103088, 103089, 103090 }));
		gemMap.put(8, new ModeGoodsCompound(8, 9, 2, 5, 100, 9000, new int[] { 103086, 103087, 103088, 103089, 103090 }, new int[] { 103091, 103092, 103093, 103094, 103095 }));

		// 强化石合成配置
		gemMap.put(goods[0], new ModeGoodsCompound(goods[1], 1, 1, 2, 100, 1000));
		gemMap.put(goods[1], new ModeGoodsCompound(goods[2], 2, 1, 2, 100, 2000));
		gemMap.put(goods[2], new ModeGoodsCompound(goods[3], 3, 1, 2, 100, 3000));
		gemMap.put(goods[3], new ModeGoodsCompound(goods[4], 4, 1, 2, 100, 4000));
		gemMap.put(goods[4], new ModeGoodsCompound(goods[5], 5, 1, 2, 100, 5000));
		gemMap.put(goods[5], new ModeGoodsCompound(goods[6], 6, 1, 2, 100, 6000));
		gemMap.put(goods[6], new ModeGoodsCompound(goods[7], 7, 1, 2, 100, 7000));
		gemMap.put(goods[7], new ModeGoodsCompound(goods[8], 8, 1, 2, 100, 8000));
		gemMap.put(goods[8], new ModeGoodsCompound(goods[9], 9, 1, 2, 100, 9000));
		gemMap.put(goods[9], new ModeGoodsCompound(goods[10], 10, 1, 2, 100, 10000));
		gemMap.put(goods[10], new ModeGoodsCompound(goods[11], 11, 1, 2, 100, 11000));

		// 附星宝箱
		boxMap.put(boxs[0], new ModeGoodsCompound(boxs[1], 5, 4, 5, 100, 1000));
		boxMap.put(boxs[1], new ModeGoodsCompound(boxs[2], 6, 4, 5, 100, 1000));
		boxMap.put(boxs[2], new ModeGoodsCompound(boxs[3], 7, 4, 5, 100, 1000));
		boxMap.put(boxs[3], new ModeGoodsCompound(boxs[4], 8, 4, 5, 100, 1000));
		boxMap.put(boxs[4], new ModeGoodsCompound(boxs[5], 9, 4, 5, 100, 1000));
	}

	private ModeGoodsCompound(int key, int level, int type, int counts, int ratio, int asset) {
		this.key = key;
		this.level = level;
		this.type = type;
		this.counts = counts;
		this.ratio = ratio;
		this.asset = asset;
	}

	private ModeGoodsCompound(int key, int level, int type, int counts, int ratio, int asset, int[] source, int[] target) {
		this.key = key;
		this.level = level;
		this.type = type;
		this.counts = counts;
		this.ratio = ratio;
		this.asset = asset;
		if (source.length == target.length) {
			for (int i = 0, j = source.length; i < j; i++) {
				resultMap.put(source[i], target[i]);
			}
		}
	}

	public static ModeGoodsCompound get(int level) {
		return gemMap.get(level);
	}

	public static ModeGoodsCompound getBox(int goodsId) {
		return boxMap.get(goodsId);
	}

	public static List<Object[]> getConfigList(int... value) {
		List<Object[]> config = new ArrayList<Object[]>();
		if (value[0] > 8) {
			boolean box = Arrays.binarySearch(boxs, value[0]) >= 0;
			int[] temp = box ? boxs : goods;
			int counts = temp.length;
			for (int i = 0; i < temp.length; i++) {
				if (value[0] == temp[i]) {
					counts = i;
					break;
				}
			}
			for (; counts < temp.length; counts++) {
				ModeGoodsCompound c = null;
				if (box) {
					c = getBox(temp[counts]);
				}
//				else {
//					c = get(temp[counts]);
//				}
				if (!BlankUtils.isBlank(c)) {
					config.add(new Object[] { c.getLevel(), c.getCounts(), c.getAsset(), Context.getGoodsServer().getModeById(c.getKey()).toGoodsVO() });
				}
			}
		} else {
			for (int i = value[0]; i <= 8; i++) {
				ModeGoodsCompound c = get(i);
				if (BlankUtils.isBlank(c)) {
					c = getBox(i);
				}
				GoodsVO goods = Context.getGoodsServer().getModeById(c.getResultMap().get(value[1])).toGoodsVO();
				value[1] = goods.getGoodsModeVO().getGoodsId();
				config.add(new Object[] { c.getLevel(), c.getCounts(), c.getAsset(), goods });
			}
		}
		return config;
	}

	public int getKey() {
		return key;
	}

	public void setKey(int key) {
		this.key = key;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	public int getRatio() {
		return ratio;
	}

	public void setRatio(int ratio) {
		this.ratio = ratio;
	}

	public int getAsset() {
		return asset;
	}

	public void setAsset(int asset) {
		this.asset = asset;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Map<Integer, Integer> getResultMap() {
		return resultMap;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public static Map<Integer, ModeGoodsCompound> getBoxMap() {
		return boxMap;
	}

}
